//https://leetcode.cn/problems/reverse-string-ii/
package codeRandomThoughts.Test541反转字符串II;

public class Solution {
    public String reverseStr(String s, int k) {
        //这个用双指针法很好理解
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i+=2*k) {
            int start = i;//双指针左侧的指针

            //判断够不够k个来决定反转结束的位置
            int end = Math.min(chars.length-1,start+k-1);

            //这里用异或运算反转
            while (start<end){
                chars[start] ^= chars[end];
                chars[end] ^= chars[start];
                chars[start]^=chars[end];
                start++;
                end--;
            }
        }
        return new String(chars);
    }
}
